package com.seafood.entity;

import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * 文件上传记录实体类
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "file_uploads")
public class FileUpload extends BaseEntity {
    
    @NotBlank(message = "原始文件名不能为空")
    @Column(name = "original_filename", nullable = false, length = 255)
    private String originalFilename;
    
    @NotBlank(message = "存储文件名不能为空")
    @Column(name = "stored_filename", nullable = false, length = 255)
    private String storedFilename;
    
    @NotBlank(message = "文件路径不能为空")
    @Column(name = "file_path", nullable = false, length = 500)
    private String filePath;
    
    @NotBlank(message = "文件URL不能为空")
    @Column(name = "file_url", nullable = false, length = 1000)
    private String fileUrl;
    
    @Column(name = "file_size")
    private Long fileSize;
    
    @Column(name = "content_type", length = 100)
    private String contentType;
    
    @NotNull(message = "文件类型不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "file_type", nullable = false)
    private FileType fileType;
    
    @NotNull(message = "业务类型不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "business_type", nullable = false)
    private BusinessType businessType;
    
    @Column(name = "business_id")
    private Long businessId;
    
    @Column(name = "uploader_id")
    private Long uploaderId;
    
    @Column(name = "uploader_name", length = 100)
    private String uploaderName;
    
    @Column(name = "file_md5", length = 32)
    private String fileMd5;
    
    @Column(name = "thumbnail_url", length = 1000)
    private String thumbnailUrl;
    
    @Column(name = "is_public")
    private Boolean isPublic = true;
    
    @Column(name = "is_deleted")
    private Boolean isDeleted = false;
    
    @Column(name = "download_count")
    private Long downloadCount = 0L;
    
    @Column(name = "description", length = 500)
    private String description;
    
    @Column(name = "tags", length = 500)
    private String tags;
    
    /**
     * 文件类型枚举
     */
    public enum FileType {
        IMAGE,      // 图片
        VIDEO,      // 视频
        DOCUMENT,   // 文档
        AUDIO,      // 音频
        ARCHIVE,    // 压缩包
        OTHER       // 其他
    }
    
    /**
     * 业务类型枚举
     */
    public enum BusinessType {
        PRODUCT_IMAGE,      // 产品图片
        PRODUCT_VIDEO,      // 产品视频
        USER_AVATAR,        // 用户头像
        COMPANY_LOGO,       // 公司logo
        CERTIFICATE,        // 证书文件
        INSPECTION_REPORT,  // 检验报告
        TRACEABILITY_DOC,   // 溯源文档
        ORDER_ATTACHMENT,   // 订单附件
        COLD_CHAIN_DOC,     // 冷链文档
        OTHER              // 其他
    }
    
    /**
     * 获取文件扩展名
     */
    public String getFileExtension() {
        if (originalFilename != null && originalFilename.contains(".")) {
            return originalFilename.substring(originalFilename.lastIndexOf("."));
        }
        return "";
    }
    
    /**
     * 是否为图片文件
     */
    public boolean isImage() {
        return FileType.IMAGE.equals(this.fileType);
    }
    
    /**
     * 增加下载次数
     */
    public void incrementDownloadCount() {
        this.downloadCount++;
    }
}